package com.tycmc.signelectroniccontracts.logic.renzheng.qiye;

import android.app.Activity;
import android.content.Intent;

import com.baidu.ocr.sdk.OCR;
import com.baidu.ocr.sdk.OnResultListener;
import com.baidu.ocr.sdk.exception.OCRError;
import com.baidu.ocr.sdk.model.IDCardParams;
import com.baidu.ocr.sdk.model.IDCardResult;
import com.baidu.ocr.sdk.model.Word;
import com.baidu.ocr.ui.camera.CameraActivity;
import com.tycmc.signelectroniccontracts.act.renzheng.qiye.FraFarenxinxi;
import com.tycmc.signelectroniccontracts.tools.base.LogicParent;
import com.tycmc.signelectroniccontracts.tools.images.UrlPath;
import com.tycmc.signelectroniccontracts.tools.net.NetUrl;
import com.tycmc.signelectroniccontracts.tools.net.NetWorkUtils;
import com.tycmc.signelectroniccontracts.tools.net.RequestAction;
import com.tycmc.signelectroniccontracts.tools.net.result.NetworkResult;
import com.tycmc.signelectroniccontracts.tools.other.TyTypenum;
import com.tycmc.signelectroniccontracts.tools.show.LogShow;
import com.tycmc.signelectroniccontracts.tools.show.ProDialog;
import com.tycmc.signelectroniccontracts.tools.show.ToastShow;
import com.tycmc.signelectroniccontracts.tools.times.DataTimer;

import java.io.File;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

public class PreFarenxinxi extends LogicParent {

    private  String filePath;
    private  FaRenxinxiInterface pre;
    private FraFarenxinxi fra;

    public PreFarenxinxi(FaRenxinxiInterface pre, String filePath) {
        this.pre = pre;
        fra = ((FraFarenxinxi) pre);
        this.filePath = filePath;
    }
    public void onResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == TyTypenum.renlianTouxiang && resultCode == Activity.RESULT_OK) {
            Serializable extra = data.getSerializableExtra(CameraActivity.KEY_CONTENT_TYPE);
            if (CameraActivity.CONTENT_TYPE_ID_CARD_FRONT.equals(extra)) {
                if (new File(UrlPath.imagePath + IDCardParams.ID_CARD_SIDE_FRONT +filePath+ ".png").exists()) {
                    pre.sendPicterBefore(UrlPath.imagePath + IDCardParams.ID_CARD_SIDE_FRONT +filePath+ ".png");
                    recode(IDCardParams.ID_CARD_SIDE_FRONT, UrlPath.imagePath + IDCardParams.ID_CARD_SIDE_FRONT +filePath+ ".png");
                }
            }
        }
        if (requestCode == TyTypenum.renlianBack && resultCode == Activity.RESULT_OK) {
            Serializable extra = data.getSerializableExtra(CameraActivity.KEY_CONTENT_TYPE);
            if (CameraActivity.CONTENT_TYPE_ID_CARD_BACK.equals(extra)) {
                pre.sendPicterEnd(UrlPath.imagePath + IDCardParams.ID_CARD_SIDE_BACK +filePath+ ".png");

            }
        }
    }

    private void recode(final String positon, final String url) {
// 行驶证识别参数设置
        IDCardParams param = new IDCardParams();

// 设置image参数
        param.setImageFile(new File(url));
// 设置其他参数
        param.setIdCardSide(positon);
        // 设置方向检测
        param.setDetectDirection(true);
        // 设置图像参数压缩质量0-100, 越大图像质量越好但是请求时间越长。 不设置则默认值为20
        param.setImageQuality(20);
        param.setDetectRisk(true);
        final ProDialog dialog = new ProDialog(fra.getActivity(), "正在识别");
        dialog.show();
        OCR.getInstance(fra.getActivity()).recognizeIDCard(param, new OnResultListener<IDCardResult>() {
            @Override
            public void onResult(IDCardResult result) {
                // 调用成功，返回OcrResponseResult对象.
                LogShow.i("成功", result.getLogId() + "\n" + result.getJsonRes());
                dialog.dismiss();
                Map map = new HashMap();
                map.put("clientName", getWords(result.getName(), ""));
                map.put("sex", getWords(result.getGender(), ""));
                map.put("nation", getWords(result.getEthnic(), ""));
                map.put("dateOfBirth", DataTimer.getStringData("yyyy-MM-dd", DataTimer.getLongTime(getWords(result.getBirthday(), ""), "yyyyMMdd")));
                map.put("idcNo", getWords(result.getIdNumber(), ""));
                map.put("address", getWords(result.getAddress(), ""));
//                initData(strxinxi, map);
                pre.initData(getWords(result.getName(), ""),getWords(result.getGender(), ""), getWords(result.getEthnic(), ""), DataTimer.getStringData("yyyy-MM-dd", DataTimer.getLongTime(getWords(result.getBirthday(), ""), "yyyyMMdd")), getWords(result.getIdNumber(), ""), getWords(result.getAddress(), ""));
            }

            @Override
            public void onError(OCRError error) {
                dialog.dismiss();
                if (new File(url).exists()) {
                    new File(url).delete();
                }
//                ToaShow.popupToast(context,error.getMessage() + "(" + error.getErrorCode() + ")");
                LogShow.i("失败", error.getLogId() + "\n" + error.getErrorCode() + "\n" + error.getMessage());
                // 调用失败，返回OCRError对象
            }
        });
    }

    private String getWords(Word word, String str) {
        if (word != null) {
            return word.getWords();
        }
        return str;
    }

    public void upMap(final Map upMap) {
        RequestAction action = new RequestAction(NetUrl.saveAgentInfo);
        action.putBodyAll(upMap);
        final ProDialog dialog = new ProDialog(fra.getActivity(), "正在上传信息");
        dialog.show();
        new NetWorkUtils(fra).postJson(action, new NetworkResult() {
            @Override
            public void sucess(Map<String, Object> data, String str) {
                dialog.dismiss();
                pre.next(upMap);
            }

            @Override
            public void failed(Object data) {
                dialog.dismiss();
                ToastShow.popupToast(data+"");
            }
        });
    }

    public interface FaRenxinxiInterface{
        void sendPicterBefore(String before);
        void sendPicterEnd(String end);
        void initData(String clientName,String sex,String nation,String dateOfBirth,String idcNo,String address);

        void next(Map upMap);
    }
}
